<template>
  <el-dialog id="edit-dialog" :title="title" :visible.sync="showState" :destroy-on-close="true" :before-close="handleClose" :close-on-click-modal="false" width="60%">
    <el-form ref="formValidate" :model="formValidate" :rules="rules" label-width="150px" class="demo-ruleForm">
      <el-form-item :label="$t('msgCenter.msgPush.table.theme')" prop="subject">
        <el-input v-model="formValidate.subject" :disabled="state === 'view'" :placeholder="$t('msgCenter.msgPush.prompt.theme')" :maxlength="50" />
      </el-form-item>
      <el-form-item :label="$t('msgCenter.msgPush.table.releaseDate')" prop="pushTime">
        <el-date-picker
          v-model="time"
          type="datetime"
          :placeholder="$t('msgCenter.msgPush.prompt.releaseDate')"
          :disabled="state === 'view'"
          style="width: 100%"
        />
      </el-form-item>
      <el-form-item :label="$t('msgCenter.msgPush.table.pushType')" prop="pushType">
        <el-select v-model="formValidate.pushType" clearable :disabled="state === 'view'" :placeholder="$t('msgCenter.msgPush.prompt.pushType')" style="width: 100%">
          <el-option
            v-for="item in $t('msgCenter.msgPush.pushType')"
            :key="item.value"
            :label="item.desc"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-row v-if="formValidate.pushType === 2">
        <el-form-item :label="$t('msgCenter.msgPush.table.pushContent')" prop="pushContent">
          <el-input
            v-model="formValidate.pushContent"
            type="textarea"
            :autosize="{ minRows: 4, maxRows: 6}"
            :placeholder="$t('msgCenter.msgPush.prompt.pushContent')"
            :maxlength="500"
            :disabled="state === 'view'"
            show-word-limit
          />
        </el-form-item>
        <el-form-item :label="$t('msgCenter.msgPush.table.pushObj')" prop="pushObj">
          <el-select v-model="formValidate.pushObj" clearable :disabled="state === 'view'" :placeholder="$t('msgCenter.msgPush.prompt.pushObj')" style="width: 100%">
            <el-option
              v-for="item in $t('msgCenter.msgPush.pushObj')"
              :key="item.value"
              :label="item.desc"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item v-if="formValidate.pushObj === 1">
          <el-upload
            ref="upload"
            class="upload-demo"
            action="#"
            :on-change="handleChange"
            :file-list="fileList"
            accept=".xls,.xlsx"
          >
            <el-button size="small" type="primary">{{ $t('msgCenter.msgPush.prompt.expLable') }}</el-button>
          </el-upload>
          <p>{{ $t('msgCenter.msgPush.prompt.exp') }}</p>
          <table class="tab">
            <tr>
              <td>{{ $t('msgCenter.msgPush.prompt.userId') }}</td>
            </tr>
            <tr>
              <td />
            </tr>
          </table>
        </el-form-item>
      </el-row>
      <el-form-item v-if="formValidate.pushType === 1">
        <el-upload
          ref="upload"
          class="upload-demo"
          action="#"
          :on-change="handleChange1"
          :file-list="fileList1"
          accept=".xls,.xlsx"
        >
          <el-button size="small" type="primary">{{ $t('msgCenter.msgPush.prompt.btn') }}</el-button>
        </el-upload>
        <p>{{ $t('msgCenter.msgPush.prompt.exple') }}</p>
        <table class="tab">
          <tr>
            <td>{{ $t('msgCenter.msgPush.prompt.userId') }}</td>
            <td>{{ $t('msgCenter.msgPush.prompt.content') }}</td>
          </tr>
          <tr>
            <td />
            <td />
          </tr>
        </table>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleClose">{{ $t('common.operate.cancel') }}</el-button>
      <el-button v-if="state !== 'view'" type="primary" :loading="submitLoading" @click="handleSubmit">{{ $t('common.operate.sure') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { updateMsgPush, addMsgPush } from '@/api/operation'
import { formatDate } from '@/utils/method'
export default {
  name: 'MsgPushDialog',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    chooseItem: {
      type: Object,
      default: function() {
        return {}
      }
    },
    title: {
      type: String,
      default: null
    },
    state: {
      type: String,
      default: null
    }
  },
  data() {
    const validateTime = (rule, value, callback) => {
      if (this.time) {
        callback()
      } else {
        callback(new Error(this.$t('msgCenter.msgPush.table.releaseDate') + this.$t('common.null')))
      }
    }
    return {
      submitLoading: false,
      time: null,
      fileList: [],
      fileList1: [],
      formValidate: {
        id: null,
        subject: null,
        pushTime: null,
        pushType: null,
        pushContent: null,
        pushObj: null,
        operatorId: null,
        pushExcel: null,
        isNotice: null
      },
      rules: {
        subject: [{ required: true, message: this.$t('msgCenter.msgPush.table.theme') + this.$t('common.null'), trigger: 'blur' }],
        pushTime: [{ required: true, validator: validateTime, trigger: 'blur' }],
        pushType: [{ required: true, message: this.$t('msgCenter.msgPush.table.pushType') + this.$t('common.null'), trigger: 'blur' }],
        pushContent: [{ required: true, message: this.$t('msgCenter.msgPush.table.pushContent') + this.$t('common.null'), trigger: 'blur' }],
        pushObj: [{ required: true, message: this.$t('msgCenter.msgPush.table.pushObj') + this.$t('common.null'), trigger: 'blur' }]
      }
    }
  },
  computed: {
    showState: {
      get: function() {
        return this.showDialog
      },
      set: function() {
      }
    }
  },
  mounted() {
    this.formValidate = this.chooseItem
  },
  methods: {
    setTime(start, end) {
      this.time = [start, end]
    },
    handleClose() {
      this.$emit('change-dialog', false)
    },
    handleSubmit() {
      this.$refs['formValidate'].validate((valid) => {
        if (valid) {
          this.submitLoading = true
          const data = this.formValidate
          const formData = new FormData()
          if (data.id) {
            formData.append('id', data.id)
          }
          formData.append('isNotice', data.isNotice)
          formData.append('subject', data.subject)
          formData.append('pushTime', formatDate(new Date(this.time), 'yyyy-MM-dd hh:mm:ss'))
          formData.append('pushType', data.pushType)
          formData.append('operatorId', JSON.parse(sessionStorage.getItem('loginInfo'))['id'])
          if (data.pushType === 1) {
            formData.append('pushObj', '1')
            if (this.fileList1.length > 0) {
              formData.append('pushExcel', this.fileList1[0])
            }
          } else {
            formData.append('pushContent', data.pushContent)
            formData.append('pushObj', data.pushObj)
            if (this.fileList.length > 0) {
              formData.append('pushExcel', this.fileList[0])
            }
          }
          if (data['id']) {
            updateMsgPush(formData)
              .then(res => {
                this.submitLoading = false
                if (res.success) {
                  this.$message.success(this.$t('common.tip.editSuccess'))
                  this.$emit('edit-success')
                  this.handleClose()
                } else {
                  this.$message.error(res.message)
                }
              })
              .catch(err => {
                this.$message.error(err.message)
                this.submitLoading = false
              })
          } else {
            addMsgPush(formData)
              .then(res => {
                this.submitLoading = false
                if (res.success) {
                  this.$message.success(this.$t('common.tip.addSuccess'))
                  this.$emit('edit-success')
                  this.handleClose()
                } else {
                  this.$message.error(res.message)
                }
              })
              .catch(err => {
                this.$message.error(err.message)
                this.submitLoading = false
              })
          }
        }
      })
    },
    handleChange(file) {
      this.fileList = [file]
    },
    handleChange1(file) {
      this.fileList1 = [file]
    }
  }
}
</script>

<style lang="scss" scoped>
  .tab{
    text-align: center;
    border-collapse:collapse;
    td{
      border: 1px solid #cccccc;
      padding-left:20px ;
      padding-right: 20px;
      height: 40px;
    }
  }
</style>
